//
//  SmsTableViewCell.m
//
//
//  Created by gynjobs on 2017/8/14.
//  Copyright © 2017年 gynjobs. All rights reserved.
//

#import "SmsTableViewCell.h"
#import "NotificationCenterManager.h"

static const NSInteger timer_sec = 90;

@interface SmsTableViewCell (){
    NSInteger timer_count;
}

@end

@implementation SmsTableViewCell

-(instancetype)init{
    
    if(self = [super initWithId:SMS_CELL_ID]){
        
        self.smsDelegate = nil;
        timer_count = timer_sec;
        
        
        self.smsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [ self.smsBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [ self.smsBtn setTitleColor:[UIColor colorWithString:UI_COLOR_MAIN_BG_3] forState:UIControlStateNormal];
         self.smsBtn.titleLabel.font = [UIFont systemFontOfSize:UI_FOND_HOME_CREDIT_CELL_MAIN_1];
        [ self.smsBtn addTarget:self action:@selector(smsBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        
         self.hLineView = [[UIView alloc] init];
         self.hLineView.backgroundColor = [UIColor colorWithString:UI_COLOR_SPLIT_LINE_1];
        
        
        [self addSubview: self.smsBtn];
        [self addSubview: self.hLineView];
        [self updateConstraintsIfNeeded];
        [self addNextObserver];
    }
    return self;
    
}


-(void)updateConstraints{
    [super updateConstraints];
    
    [ self.smsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.mas_right).with.offset(-LEFT_RIGHT_SPACE);
        make.centerY.equalTo(self.mas_centerY);
        make.height.mas_equalTo(CONTENT_TEXTFIELD_HEIGHT);
        
    }];
    
    [ self.hLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo( self.smsBtn.mas_left).with.offset(-LEFT_RIGHT_SPACE);
        make.centerY.equalTo(self.mas_centerY);
        make.height.mas_equalTo(CONTENT_TEXTFIELD_HEIGHT);
        make.width.mas_equalTo(SPLIT_LINE_HEIGHT);
        
    }];
//    UIView * supev  = [self.contentTextfield superview];
    [ self.contentTextfield mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo( self.hLineView.mas_left).with.offset(-LEFT_RIGHT_SPACE);
        make.left.equalTo( self.titleLabel.mas_right).with.offset(LEFT_RIGHT_SPACE);
        make.centerY.equalTo(self.mas_centerY);
        make.height.mas_equalTo(CONTENT_TEXTFIELD_HEIGHT);
    }];
    
}

-(void)addNextObserver{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(invalidateTimer) name:LOAN_STEP1_NEXT_NOTIFICATION object:nil];
}


#pragma mark -
#pragma mark sms btn pressed

-(void)smsBtnPressed:(id)sender{
    
    [[self superview] endEditing:YES];
    
    
    
    if( self.smsDelegate && [ self.smsDelegate respondsToSelector:@selector(loanSmsCell: withCallBack:)]){
        
        [ self.smsDelegate loanSmsCell:self withCallBack:^(BOOL isMobile,id error_msg) {
            
            if(isMobile){
                
                self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(udpateTime:) userInfo:nil repeats:YES];
                [[NSRunLoop currentRunLoop] addTimer: self.timer forMode:UITrackingRunLoopMode];
                 self.smsBtn.userInteractionEnabled = NO;
                NSString * secStr = [NSString stringWithFormat:@"%ld s",(long)timer_sec];
                [ self.smsBtn setTitle:secStr forState:UIControlStateNormal];
                
            }else{
                
                if(error_msg){
                    
                    [self invalidateTimer];
                    return ;
                }
                
                //                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入手机号" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                //                [alert show];
                
            }
            
            
        }];
        
    }
    
    
}


-(void)udpateTime:(id) userInfo{
    
    timer_count --;
    
    if(timer_count > 0){
        
        NSString * secStr = [NSString stringWithFormat:@"%ld s",(long)timer_count];
        [ self.smsBtn setTitle:secStr forState:UIControlStateNormal];
        
    }else{
        
        [self invalidateTimer];
    }
    
    
}

-(void)invalidateTimer{
    
    [ self.timer invalidate];
    timer_count = timer_sec;
     self.smsBtn.userInteractionEnabled = YES;
    [ self.smsBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    
}


-(void)dealloc{
    
    [ self.timer invalidate];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
